package com.ctn.information.menu;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ctn.information.R;
import com.ctn.information.SeachNewsActivity;
import com.ctn.information.SetActivity;

/**
 * 右边菜单栏View Item Fragment
 * 
 * @author HLJ
 *
 */
public class MenuSystemFragment extends Fragment {
	
	private Context currentcontext;
	
	public MenuSystemFragment(Context context) { 
		currentcontext=context;
	}
	
	ListView list = null; 
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		LinearLayout view =(LinearLayout) inflater.from(getActivity()).inflate(R.layout.system_properties, null);
		list = (ListView)view.findViewById(R.id.mine_list);
		return view;
	}
	
	/**
	 * 对应右边菜单栏中的设置，离线下载，夜间模式，资讯搜索图标
	 */
	private int[] icon = new int[]{R.drawable.setting,
								   R.drawable.download,
								   R.drawable.moon,
								   R.drawable.search};
	
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		SysItemAdapter adapter = new SysItemAdapter(getActivity());
		String[] topic = getResources().getStringArray(R.array.sys_menu_names);
		for (int i = 0; i < topic.length; i++) {
			SysItem item = new SysItem(topic[i],icon[i]);
			adapter.add(item);
		}
		list.setAdapter(adapter);
		list.setOnItemClickListener(new ListOnItemClickListener());
	}
	
	private class SysItem {
		public String tag;
		public int iconRes;
		public SysItem(String tag, int iconRes) {
			this.tag = tag; 
			this.iconRes = iconRes;
		}
	}

	public class SysItemAdapter extends ArrayAdapter<SysItem> {

		public SysItemAdapter(Context context) {
			super(context, 0);
		}

		@SuppressLint("ResourceAsColor") public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.sys_row, null);
			}
			
			ImageView icon = (ImageView) convertView.findViewById(R.id.sys_row_icon);
			icon.setImageResource(getItem(position).iconRes);
			
			TextView title = (TextView) convertView.findViewById(R.id.sys_row_title);
			title.setText(getItem(position).tag);
			
			return convertView;
		}

	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}
	private class ListOnItemClickListener implements OnItemClickListener{
		
			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				TextView title = (TextView) v.findViewById(R.id.sys_row_title);
				Log.i("ListOnItemClickListener", title.getText().toString());
				String titleName=title.getText().toString();
				if(getString(R.string.sys_list_setting).equalsIgnoreCase(titleName)){
					Intent intent =new Intent(currentcontext, SetActivity.class);
					startActivity(intent);
				}else if(getString(R.string.sys_list_search_news).equalsIgnoreCase(titleName)){
						Intent intent =new Intent(currentcontext, SeachNewsActivity.class);
						startActivity(intent);
					
				}
				/*TextView tvTitle = (TextView) v
						.findViewById(R.id.rss_item_title);
				TextView tvUrl = (TextView) v.findViewById(R.id.rss_item_url);
				TextView tvIsCnblogs = (TextView) v
						.findViewById(R.id.rss_item_is_cnblogs);
				TextView tvAuthor = (TextView) v
						.findViewById(R.id.rss_item_author);
				String title = tvTitle.getText().toString();
				String url = tvUrl.getText().toString();
				boolean isCnblogs = tvIsCnblogs.getText().toString()
						.equals("1");
				String author = tvAuthor.getText().toString();

				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				if (isCnblogs) {// 博客园
					intent.setClass(RssListActivity.this,
							AuthorBlogActivity.class);
					bundle.putString("blogName", title);
					bundle.putString("author", author);
				} else {
					intent.setClass(RssListActivity.this,
							RssItemsActivity.class);
					bundle.putString("title", title);
					bundle.putString("url", url);
				}

				intent.putExtras(bundle);

				startActivity(intent);*/
			}
		
	}
}
